/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function( $, common, ready, Collection, template, Model, dialog ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'limitbuys',
                condition: {},
                model: {
                    url:_global.url.api + 'limitbuy',
                    event: {
                        '.item-remove:click': 'remove'
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var _this = this;
                        var itemObj = $('.remove-' + _this.data.id).parent().parent();
                        dialog({
                            content: '确定要删除吗？',
                            ok: function () {
                                _this.data.action = 'del';

                                if (!_this.put()) {
                                    return false;
                                }
                                itemObj.fadeOut("slow");
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //查询
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });

        }
    };

    new index();

});